package com.cao.meet.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextPaint;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.cao.meet.R;

public class TextImageActivity extends AppCompatActivity {

    private String content = "<p>1、给狗狗吃早餐时，你应该根据它的食量将食物分成两份，如果它一天要吃两顿晚餐也要同样这么处理。</p>\n" +
            " \n" +
            "<p><img alt=\"\" src=\"http://pic.ibreed.com.cn/admin/pic/201711/00300d5ad326a1dcff78d598002b267c.jpg\" style=\"width: 1053px;\" /></p>\n" +
            " \n" +
            "<p>2、先给它一半的食物。</p>\n" +
            " \n" +
            "<p><img alt=\"\" src=\"http://pic.ibreed.com.cn/admin/pic/201711/97098cb9842ce61a009d3cc9a2342cf7.jpg\" style=\"width: 1053px;\" /></p>\n" +
            " \n" +
            "<p>3、如果它很快地吃完了你给的那一半食物，那么将另外一半也给它。</p>\n" +
            " \n" +
            "<p><img alt=\"\" src=\"http://pic.ibreed.com.cn/admin/pic/201711/4d58ce033d3e546e747f39fc49ec6754.jpg\" style=\"width: 1053px;\" /></p>\n" +
            " \n" +
            "<p>4、20分钟之后，将狗狗还没有吃完的食物拿走并且扔掉，在下一次喂食之前不要再给它任何食物吃，包括任何奖赏。当然，你起码应该提供充足的、清洁的水给它饮用。</p>\n" +
            " \n" +
            "<p><img alt=\"\" src=\"http://pic.ibreed.com.cn/admin/pic/201711/a84ec2bb94457351ad5d503a7c69bf4b.jpg\" style=\"width: 1053px;\" /></p>\n";
    private LinearLayout llTextImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_image);
        llTextImage = (LinearLayout) findViewById(R.id.ll_textImage);
        replace();
    }

    private void replace() {
        String s = content.replaceAll("</p>", "")
                .replaceAll("\r", "")
                .replaceAll("\n", "")
                .replaceAll("<br />", "\n")
                .replaceAll("”", "\"")
                .replaceAll("“", "\"")
                .replaceAll("—", "—")
                .replaceAll("—", "—")
                .replaceAll("…", "…")
                .replaceAll(" ", " ")
                .replaceAll("·", ".")
                .replaceAll("<ul>", "")
                .replaceAll("</li>", "")
                .replaceAll("</ul>", "")
                .replaceAll("<li>", "\n•  ");
        Log.e("s",s);
        String[] split = s.split("<p>");
        for (int x = 0; x < split.length; x++) {
            String str = split[x];
            if (x == 0) {
                continue;
            }
            if (str.indexOf("img") == -1) {
                TextView textView = new TextView(this);
                if (str.indexOf("strong") != -1) {
                    TextPaint tp = textView.getPaint();
                    tp.setFakeBoldText(true);
                    str = str.replaceAll("</strong>", "").replaceAll("<strong>", "");
                }
                textView.setText(str);
                textView.setTextColor(this.getResources().getColor(R.color.item_text_color1));
                textView.setTextSize(14);
                textView.setLineSpacing(0, 1.5f);
                textView.setPadding(0, dip2px(this, 20), 0, 0);
                llTextImage.addView(textView);
            } else {
                final ImageView imageView = new ImageView(this);
                llTextImage.addView(imageView);
                final String finalStr = str;
                Glide.with(this).load(Uri.parse("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2057887804,201097851&fm=26&gp=0.jpg")).asBitmap().into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        if (resource != null) {
                            int x = getWindow().getWindowManager().getDefaultDisplay().getWidth() - dip2px(TextImageActivity.this, 24);
                            int i = (resource.getHeight()) * x / (resource.getWidth());
                            Glide.with(TextImageActivity.this).load(Uri.parse("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1593875525,3034004494&fm=26&gp=0.jpg")).into(imageView);
                            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                            params.setMargins(0, dip2px(TextImageActivity.this, 10), 0, 0);
                            params.width = x;
                            params.height = i;
                            imageView.setLayoutParams(params);
                        }
                    }
                });
            }
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
